package com.czp.ssm.controller;

import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.czp.ssm.bean.Company;
import com.czp.ssm.bean.Example;
import com.czp.ssm.bean.Msg;
import com.czp.ssm.bean.User;
import com.czp.ssm.bean.UserCard;
import com.czp.ssm.service.CompanyService;
import com.czp.ssm.service.UserCardService;
import com.czp.ssm.service.UserService;
import com.czp.ssm.utils.CaptchaUtil;

@Controller
@RequestMapping("/userController")  
public class UserController {

	
	@Autowired
	private UserService userService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private UserCardService userCardService;
	
	/**
	 * 添加头索引
	 * @return   
	 */
	@RequestMapping("/toHeadIndex")
	public String toHeadIndex() {
		return "/foreground/user/headIndex";
	}
	
	/**
	 * 登陆页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/toUserLogin")
	public String toUserLogin() {
		return "/foreground/user/login";
	}
	
	/**
	 * 注册页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/toRegister")
	public String toRegister() {
		return "/foreground/user/register";
	}
	
	/**
	 * 忘记密码页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/toAlterPassword")
	public String toAlterPassword() {
		return "/foreground/user/alterPassword";
	}
	
	
	/**
	 * 后台登陆页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/toSuxingAdminView")
	public String toSuXingAdminView() {
		return "/background/admin/login";
	}
	
	
	/**
	 * 后台管理页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/toAdminIndex")
	public String toAdminIndex() {
		return "/background/index";
	}
	
	
	/**
	 * 后台管理页面头部
	 * @param user
	 * @return
	 */
	@RequestMapping("/topView")
	public String topView() {
		return "/background/top";
	}
	
	/**
	 * 后台管理页面左边导航
	 * @param user
	 * @return
	 */
	@RequestMapping("/leftView")
	public String leftView() {
		return "/background/left";
	}
	
	/**
	 * 后台管理页面右方显示页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/rightView")
	public String rightView() {
		return "/background/right";
	}
	
	/**
	 * 后台管理页面右方显示页面
	 * @param user
	 * @return
	 */
	@RequestMapping("/bottomView")
	public String bottomView() {
		return "/background/bottom";
	}
	
	/**
	 * 验证登陆
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/login" , method=RequestMethod.POST)
	public String login(Example example) {
		System.out.println("userId----"+example.getUserId());
		System.out.println("userName----"+example.getUserPwd());
		Subject subject = SecurityUtils.getSubject();
		
		if (!subject.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(example.getUserId(), example.getUserPwd());
			token.setRememberMe(true);
			try {
				System.out.println("------"+token.hashCode());
				subject.login(token);
			} catch (UnknownAccountException uae) {  
	            return "error username";  
	        } catch (IncorrectCredentialsException ice) {  
	            return "error password";  
	        }  
			
		}
		return "/background/bottom";
	}
	
	@RequestMapping(value="/login1" , method=RequestMethod.POST)
	public String login1(@RequestParam("userId")String userId,@RequestParam("userPwd")String userPwd) {
		Subject subject = SecurityUtils.getSubject();
		
		if (!subject.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(userId, userPwd);
			token.setRememberMe(true);
			try {
				System.out.println("------"+token.hashCode());
				subject.login(token);
			} catch (UnknownAccountException uae) {  
	            return "error username";  
	        } catch (IncorrectCredentialsException ice) {  
	            return "error password";  
	        }  
			
		}
		return "/background/bottom";
	}
	
	@RequestMapping(value="/findUserByUserNameAndCompanyId" , method=RequestMethod.POST)
	@ResponseBody
	public Msg findUserByUserNameAndCompanyId(@RequestParam("companyId")String companyId,@RequestParam("userName")String userName) {
		Company company = companyService.findCompanyByCompanyId(companyId);
		List<User> userList = userService.findUserByUserNameAndCompanyId(companyId,userName);
		
		return Msg.success().add("company", company).add("userList", userList);
	}
	
	
	
	
	
	@RequestMapping(value="/findCardByUserIdAndCardtype" , method=RequestMethod.POST)
	@ResponseBody
	public Msg findCardByUserIdAndCardtype(@RequestParam("cardType")String cardType,@RequestParam("userId")String userId) {
		System.out.println(cardType);
		UserCard userCard = userCardService.getCardByUserIdAndCardtype(cardType,userId);
		System.out.println(userCard);
		return Msg.success().add("userCard", userCard);
	}
	
	
	/**
	 * 员工管理
	 * @return
	 */
	@RequestMapping("/userManage")
	public String userManage() {
		return "/background/bottom";
	}
	
	/**
	 * 生成验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
    @RequestMapping("/createCode")  
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {  
    	// 通知浏览器不要缓存  
        response.setHeader("Expires", "-1");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setHeader("Pragma", "-1");  
        CaptchaUtil util = CaptchaUtil.Instance();  
        // 将验证码输入到session中，用来验证  
        String code = util.getString();  
        request.getSession().setAttribute("code", code);  
        // 输出打web页面  
        ImageIO.write(util.getImage(), "jpg", response.getOutputStream());  
    }  
}
